﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Simhopp.Database;


namespace Simhopp.Model
{
    public class CompetitionList: InterfaceCompetitionListModel
    {
        public  List<Competition> competitionList { get; set;}

        public CompetitionList()
        {
            competitionList = new List<Competition>();
        }

        public CompetitionList InitializeCompetitionList()
        {
            DBConnect dbc = new DBConnect();
            
                
                competitionList = dbc.GetAllCompetition(competitionList);
            
            foreach(var comp in competitionList)
            {
                GetJudgesToCompetition(comp, dbc);
                GetDiversToCompetition(comp, dbc);
                comp.GetDivesForDiver(dbc);
                comp.DiverList.Sort((x, y) => x.StartNumber.CompareTo(y.StartNumber));
                foreach(var diver in comp.DiverList)
                {
                    diver.DiveList.Sort((x, y) => x.DiveNumber.CompareTo(y.DiveNumber));
                }
            }

            return this;
        }

        private void GetJudgesToCompetition(Competition competition, DBConnect dbc)
        {
            //DBConnect dbc = new DBConnect();
            dbc.GetJudgesWithCompetitionID(competition.ID, competition.JudgeList);
        }

        private void GetDiversToCompetition(Competition competition, DBConnect dbc)
        {
            //DBConnect dbc = new DBConnect();
            dbc.GetDiverWithCompetitionID(competition.ID, competition.DiverList);
        }

        public void AddCompetition(Competition competition)
        {
            competitionList.Add(competition);
            DBConnect dbc = new DBConnect();
            int ID = dbc.AddCompetition(competition.Name,
                competition.Event,
                competition.Location,
                competition.Date,
                competition.Height,
                competition.AgeClass,
                competition.Gender,
                competition.JudgingJudges,
                competition.RoundsInCompetition);
            competition.ID = ID;
        }

        public void RemoveCompetition(Competition competition)
        {
            competitionList.Remove(competition);
        }

        public void UpdateCompetition(Competition updatedCompetition, 
            Competition originalCompetition)
        {
            originalCompetition.Name = updatedCompetition.Name;
            originalCompetition.JudgingJudges = updatedCompetition.JudgingJudges;
            originalCompetition.Height = updatedCompetition.Height;
            originalCompetition.Location = updatedCompetition.Location;
            originalCompetition.RoundsInCompetition = updatedCompetition.RoundsInCompetition;
            originalCompetition.Gender = updatedCompetition.Gender;
            originalCompetition.AgeClass = updatedCompetition.AgeClass;
            originalCompetition.Date = updatedCompetition.Date;
            originalCompetition.Event = updatedCompetition.Event;
        }
        public CompetitionList GetCompetitionList()
        {
            return this;
        }
       

        
    }
}
